package com.starchen.optimalselection.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starchen.optimalselection.common.mapper.AttributeValueMapper;
import com.starchen.optimalselection.common.model.AttributeValue;
import com.starchen.optimalselection.common.model.SpuInfo;
import com.starchen.optimalselection.common.model.vo.ItemAttrGroupWithAttrVO;
import com.starchen.optimalselection.common.service.AttributeValueService;
import com.starchen.optimalselection.common.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service("attributeValueService")
public class AttributeValueServiceImpl extends ServiceImpl<AttributeValueMapper, AttributeValue> implements AttributeValueService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Override
    public void saveAttributeValues(List<AttributeValue> collect) {
        this.saveBatch(collect);
    }

    @Override
    public List<AttributeValue> baseAttrlistforspu(Long spuId) {
        List<AttributeValue> entities = this.baseMapper.selectList(new QueryWrapper<AttributeValue>().eq("spu_id", spuId));
        return entities;
    }

    @Transactional
    @Override
    public void updateSpuAttr(Long spuId, List<AttributeValue> entities) {
        //1、删除这个spuId之前对应的所有属性
        this.baseMapper.delete(new QueryWrapper<AttributeValue>().eq("spu_id",spuId));

        List<AttributeValue> collect = entities.stream().map(item -> {
            item.setSpuId(spuId);
            return item;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    @Override
    public List<ItemAttrGroupWithAttrVO> getAttrsWithAttrGroupBySpuId(Long spuId) {
        SpuInfo spuInfoEntity = spuInfoService.getById(spuId);
        //return this.baseMapper.getAttrsWithAttrGroup(spuId, spuInfoEntity.getCatelogId());
        return null;
    }

}